﻿using Apple.BLL.Dto.DataDto;
using Apple.BLL.Manager;
using Apple.Helper;
using HZY.Framework.DependencyInjection;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apple.SprayingProcessSystem
{
    public partial class PageChartManage : UIPage, ISingletonSelfDependency
    {
        private readonly DataManager _dataManager;

        public PageChartManage(DataManager dataManager)
        {
            InitializeComponent();

            this._dataManager = dataManager;

            this.dtp_Start.Value = DateTime.Now.AddHours(-1);
            this.dtp_End.Value = DateTime.Now;

            UILineOption option = new UILineOption
            {
                ToolTip = { Visible = true }, // ToolTip属性是只读的，但是{ Visible = true }可以修改它指向的属性值
                Title = new UITitle { Text = "" }, // 设置标题
                Legend = new UILegend  // 图例
                {
                    Orient = UIOrient.Horizontal, // 图例水平排列
                    Top = UITopAlignment.Top, // 图例位于图表的顶部
                    Left = UILeftAlignment.Left,
                },
                XAxisType = UIAxisType.DateTime, //X轴类型为日期
                XAxis = { Name = "日期", AxisLabel = { DateTimeFormat = "HH:mm:ss" } }, // X轴名称,DateTimeFormat 日期格式化
                YAxis = { Name = "数值", AxisLabel = { DecimalPlaces = 1 } }  // Y轴名称，DecimalPlaces:1 保留1位小数
            };

            string[] legendData = {
                        "脱脂喷淋泵压力值",
                        "脱脂pH值",
                        "粗洗喷淋泵压力值",
                        "陶化喷淋泵压力值",
                        "精洗喷淋泵压力值",
                        "水分炉测量温度",
                        "固化炉测量温度"
            };

            foreach (string data in legendData)
            {
                option.Legend.AddData(data);
                AddSeries(option, data);
            }

            // 设置option
            lc_Chart.SetOption(option);
        }

        private void AddSeries(UILineOption option, string seriesName)
        {
            var series = option.AddSeries(new UILineSeries(seriesName));
            series.Symbol = UILinePointSymbol.Circle; // 图标为圆
            series.SymbolSize = 4; // 图标大小
            series.SymbolLineWidth = 2; // 图标宽度
            // series.YAxisDecimalPlaces = 1;  // 小数位数
            series.Smooth = true; // 是否平滑
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btn_QueryData_Click(object sender, EventArgs e)
        {
            var t1 = this.dtp_Start.Value;
            var t2 = this.dtp_End.Value;

            if (t1 >= t2)
            {
                UIMessageTip.ShowWarning("开始时间不能大于结束时间！");
                return;
            }

            QueryDataDto dto = new QueryDataDto
            {
                StartTime = t1,
                EndTime = t2
            };

            var res = await _dataManager.GetDataListByTimeAsync(dto);

            // 每次取最后50条
            res.Data =res.Data.OrderByDescending(a => a.Id).Take(50).ToList();

            if (res.Status == SystemEnums.Result.Success)
            {
                for (int i = 0; i < res.Data.Count; i++)
                {
                    //if (i > 30)
                    //{
                    //    break;
                    //}

                    //"脱脂喷淋泵压力值",
                    //    "脱脂pH值",
                    //    "粗洗喷淋泵压力值",
                    //    "陶化喷淋泵压力值",
                    //    "精洗喷淋泵压力值",
                    //    "水分炉测量温度",
                    //    "固化炉测量温度"

                    var dataRes = res.Data[i];
                    var datetime = dataRes.InsertTime;

                    lc_Chart.Option.AddData("脱脂喷淋泵压力值", datetime, double.Parse(dataRes.脱脂喷淋泵压力值));
                    lc_Chart.Option.AddData("脱脂pH值", datetime, double.Parse(dataRes.脱脂pH值));
                    lc_Chart.Option.AddData("粗洗喷淋泵压力值", datetime, double.Parse(dataRes.粗洗喷淋泵压力值));
                    lc_Chart.Option.AddData("陶化喷淋泵压力值", datetime, double.Parse(dataRes.陶化喷淋泵压力值));
                    lc_Chart.Option.AddData("精洗喷淋泵压力值", datetime, double.Parse(dataRes.精洗喷淋泵压力值));
                    lc_Chart.Option.AddData("水分炉测量温度", datetime, double.Parse(dataRes.水分炉测量温度));
                    lc_Chart.Option.AddData("固化炉测量温度", datetime, double.Parse(dataRes.固化炉测量温度));

                }
            }

            // 刷新图表
            lc_Chart.Refresh();
        }

        #region 窗体切换卡顿处理
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams paras = base.CreateParams;
                paras.ExStyle |= 0x02000000;
                return paras;
            }
        }
        #endregion
    }
}
